import { createRouter, createWebHistory } from 'vue-router';
import Login from '@/view/login.vue';
import Home from '@/view/index.vue';
import {useTokenStore} from "@/stores/token.js";

const routes = [
    {
        path: '/login',
        name: 'Login',
        component: Login
    },
    {
        path: '/',
        name: 'Home',
        component: Home
    }
];

const router = createRouter({
    history: createWebHistory(),
    routes
});

router.beforeEach((to, from, next) => {
    const tokenStore = useTokenStore()
    const whiteList = ['/login']
    if (whiteList.includes(to.path)) {
        next()
        return
    }
    if (!tokenStore.token) {
        next('/login')
        return
    }
    next()
})

export default router;
